<?php
header("Content-Type:json/html;charset=utf8;");

$config = require '../config.php';

$username  = $_POST['username'];
$password  = $_POST['password'];


$conn = new mysqli($config['servername'], $config['username'], $config['password'], $config['dbname']);
$userSql = "select id,username,email,address,register_time,type from users where '$username' = username and '$password' = password";
$userData = $conn->query($userSql);
// 判断账号密码是否存在 存在则生成md5的随机token
if($userData->num_rows>=1){
//       更新token
    $token = md5($username.$password.time());
    $sql = "update users set token = '$token' where '$username' = username and '$password' = password";
    $result = $conn->query($sql);
    $conn->close();
    if($result){
        $data = array();
        while ($row = $userData->fetch_assoc())
        {
            $row['token'] = $token;
            $data[] = $row;
        }
        $result = ['code' =>1, 'data' => $data,  'msg' => "登录成功!"];
    }
}else{
    $result = ['code' =>0, 'msg' => "登录失败,请检查账号密码后重试!"];
}
print_r(json_encode($result)) ;
